package com.atguigu.yygh.cmn.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Api(value = "数据字典接口", tags = "字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictMapper dictMapper;

    /**
     * 根据数据id查询子数据列表
     */
    @ApiOperation(value = "根据数据id查询子数据列表", notes = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    /**
     * 数据字典信息导出
     */

    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response) throws IOException {
        dictService.exportDictData(response);
        //return Result.ok();
    }
    /**
     * 数据字典信息导入
     */
    @PostMapping("/importData")
    public Result importDict(@RequestParam("file") MultipartFile file) throws IOException {
        dictService.importDictData(file);

        return Result.ok();
    }

    /**
     * 数据字典信息导出
     */
//    @GetMapping("/export")
//    public void export(HttpServletResponse response) throws IOException {
//        // 设置浏览器响应的格式
//        //response为HttpServletResponse对象
////        response.setContentType("application/vnd.ms-excel;charset=utf-8");
////        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
////        response.setHeader("Content-Disposition", "attachment;filename=test.xls");
//
//        // 设置浏览器响应的格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        String fileName = URLEncoder.encode("用户信息", "UTF-8");
//        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
//
//        List<Dict> list = dictMapper.selectList(null);//查询所有数据
//
////        List<DictEeVo> dictEeVoList = new ArrayList<>();
////        for (Dict dict : list) {
////            DictEeVo dictEeVo = new DictEeVo();
////            BeanUtils.copyProperties(dict, dictEeVo);
////            dictEeVoList.add(dictEeVo);
////        }
//
//        ExcelWriter writer1 = ExcelUtil.getWriter(true);
//        writer1.write(list, true);
//        ServletOutputStream out = response.getOutputStream();
//        writer1.flush(out, true);
//        writer1.close();
//        IoUtil.close(out);
//
//
//    }


}
